home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / solib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-02  |  33.0 KB  |  1,253 lines

  1. /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
  2.    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
  3.    
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "defs.h"
  22.  
  23. #include <sys/types.h>
  24. #include <signal.h>
  25. #include <string.h>
  26. #include <link.h>
  27. #include <sys/param.h>
  28. #include <fcntl.h>
  29.  
  30. #ifndef SVR4_SHARED_LIBS
  31.  /* SunOS shared libs need the nlist structure.  */
  32. #include <a.out.h> 
  33. #endif
  34.  
  35. #include "symtab.h"
  36. #include "bfd.h"
  37. #include "symfile.h"
  38. #include "objfiles.h"
  39. #include "gdbcore.h"
  40. #include "command.h"
  41. #include "target.h"
  42. #include "frame.h"
  43. #include "regex.h"
  44. #include "inferior.h"
  45.  
  46. #define MAX_PATH_SIZE 256        /* FIXME: Should be dynamic */
  47.  
  48. /* On SVR4 systems, for the initial implementation, use main() as the
  49.    "startup mapping complete" breakpoint address.  The models for SunOS
  50.    and SVR4 dynamic linking debugger support are different in that SunOS
  51.    hits one breakpoint when all mapping is complete while using the SVR4
  52.    debugger support takes two breakpoint hits for each file mapped, and
  53.    there is no way to know when the "last" one is hit.  Both these
  54.    mechanisms should be tied to a "breakpoint service routine" that
  55.    gets automatically executed whenever one of the breakpoints indicating
  56.    a change in mapping is hit.  This is a future enhancement.  (FIXME) */
  57.  
  58. #define BKPT_AT_MAIN 1
  59.  
  60. /* local data declarations */
  61.  
  62. #ifndef SVR4_SHARED_LIBS
  63.  
  64. #define DEBUG_BASE "_DYNAMIC"
  65. #define LM_ADDR(so) ((so) -> lm.lm_addr)
  66. #define LM_NEXT(so) ((so) -> lm.lm_next)
  67. #define LM_NAME(so) ((so) -> lm.lm_name)
  68. static struct link_dynamic dynamic_copy;
  69. static struct link_dynamic_2 ld_2_copy;
  70. static struct ld_debug debug_copy;
  71. static CORE_ADDR debug_addr;
  72. static CORE_ADDR flag_addr;
  73.  
  74. #else    /* SVR4_SHARED_LIBS */
  75.  
  76. #define DEBUG_BASE "_r_debug"
  77. #define LM_ADDR(so) ((so) -> lm.l_addr)
  78. #define LM_NEXT(so) ((so) -> lm.l_next)
  79. #define LM_NAME(so) ((so) -> lm.l_name)
  80. static struct r_debug debug_copy;
  81. char shadow_contents[BREAKPOINT_MAX];    /* Stash old bkpt addr contents */
  82.  
  83. #endif    /* !SVR4_SHARED_LIBS */
  84.  
  85. struct so_list {
  86.   struct so_list *next;            /* next structure in linked list */
  87.   struct link_map lm;            /* copy of link map from inferior */
  88.   struct link_map *lmaddr;        /* addr in inferior lm was read from */
  89.   CORE_ADDR lmend;            /* upper addr bound of mapped object */
  90.   char so_name[MAX_PATH_SIZE];        /* shared object lib name (FIXME) */
  91.   char symbols_loaded;            /* flag: symbols read in yet? */
  92.   char from_tty;            /* flag: print msgs? */
  93.   bfd *so_bfd;                /* bfd for so_name */
  94.   struct objfile *objfile;        /* objfile for loaded lib */
  95.   struct section_table *sections;
  96.   struct section_table *sections_end;
  97.   struct section_table *textsection;
  98. };
  99.  
  100. static struct so_list *so_list_head;    /* List of known shared objects */
  101. static CORE_ADDR debug_base;        /* Base of dynamic linker structures */
  102. static CORE_ADDR breakpoint_addr;    /* Address where end bkpt is set */
  103.  
  104. /* Local function prototypes */
  105.  
  106. static void
  107. special_symbol_handling PARAMS ((struct so_list *));
  108.  
  109. static void
  110. sharedlibrary_command PARAMS ((char *, int));
  111.  
  112. static int
  113. enable_break PARAMS ((void));
  114.  
  115. static int
  116. disable_break PARAMS ((void));
  117.  
  118. static void
  119. info_sharedlibrary_command PARAMS ((char *, int));
  120.  
  121. static int
  122. symbol_add_stub PARAMS ((char *));
  123.  
  124. static struct so_list *
  125. find_solib PARAMS ((struct so_list *));
  126.  
  127. static struct link_map *
  128. first_link_map_member PARAMS ((void));
  129.  
  130. static CORE_ADDR
  131. locate_base PARAMS ((void));
  132.  
  133. static void
  134. solib_map_sections PARAMS ((struct so_list *));
  135.  
  136. #ifdef SVR4_SHARED_LIBS
  137.  
  138. static int
  139. look_for_base PARAMS ((int, CORE_ADDR));
  140.  
  141. static CORE_ADDR
  142. bfd_lookup_symbol PARAMS ((bfd *, char *));
  143.  
  144. #else
  145.  
  146. static void
  147. solib_add_common_symbols PARAMS ((struct rtc_symb *, struct objfile *));
  148.  
  149. #endif
  150.  
  151. /*
  152.  
  153. LOCAL FUNCTION
  154.  
  155.     solib_map_sections -- open bfd and build sections for shared lib
  156.  
  157. SYNOPSIS
  158.  
  159.     static void solib_map_sections (struct so_list *so)
  160.  
  161. DESCRIPTION
  162.  
  163.     Given a pointer to one of the shared objects in our list
  164.     of mapped objects, use the recorded name to open a bfd
  165.     descriptor for the object, build a section table, and then
  166.     relocate all the section addresses by the base address at
  167.     which the shared object was mapped.
  168.  
  169. FIXMES
  170.  
  171.     In most (all?) cases the shared object file name recorded in the
  172.     dynamic linkage tables will be a fully qualified pathname.  For
  173.     cases where it isn't, do we really mimic the systems search
  174.     mechanism correctly in the below code (particularly the tilde
  175.     expansion stuff?).
  176.  */
  177.  
  178. static void
  179. solib_map_sections (so)
  180.      struct so_list *so;
  181. {
  182.   char *filename;
  183.   char *scratch_pathname;
  184.   int scratch_chan;
  185.   struct section_table *p;
  186.   
  187.   filename = tilde_expand (so -> so_name);
  188.   make_cleanup (free, filename);
  189.   
  190.   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  191.             &scratch_pathname);
  192.   if (scratch_chan < 0)
  193.     {
  194.       scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
  195.                 O_RDONLY, 0, &scratch_pathname);
  196.     }
  197.   if (scratch_chan < 0)
  198.     {
  199.       perror_with_name (filename);
  200.     }  
  201.  
  202.   so -> so_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
  203.   if (!so -> so_bfd)
  204.     {
  205.       error ("Could not open `%s' as an executable file: %s",
  206.          scratch_pathname, bfd_errmsg (bfd_error));
  207.     }
  208.   if (!bfd_check_format (so -> so_bfd, bfd_object))
  209.     {
  210.       error ("\"%s\": not in executable format: %s.",
  211.          scratch_pathname, bfd_errmsg (bfd_error));
  212.     }
  213.   if (build_section_table (so -> so_bfd, &so -> sections, &so -> sections_end))
  214.     {
  215.       error ("Can't find the file sections in `%s': %s", 
  216.          exec_bfd -> filename, bfd_errmsg (bfd_error));
  217.     }
  218.  
  219.   for (p = so -> sections; p < so -> sections_end; p++)
  220.     {
  221.       /* Relocate the section binding addresses as recorded in the shared
  222.      object's file by the base address to which the object was actually
  223.      mapped. */
  224.       p -> addr += (CORE_ADDR) LM_ADDR (so);
  225.       p -> endaddr += (CORE_ADDR) LM_ADDR (so);
  226.       so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
  227.       if (strcmp (p -> sec_ptr -> name, ".text") == 0)
  228.     {
  229.       so -> textsection = p;
  230.     }
  231.     }
  232. }
  233.  
  234. /* Read all dynamically loaded common symbol definitions from the inferior
  235.    and add them to the minimal symbol table for the shared library objfile.  */
  236.  
  237. #ifndef SVR4_SHARED_LIBS
  238.  
  239. static void
  240. solib_add_common_symbols (rtc_symp, objfile)
  241.     struct rtc_symb *rtc_symp;
  242.     struct objfile *objfile;
  243. {
  244.   struct rtc_symb inferior_rtc_symb;
  245.   struct nlist inferior_rtc_nlist;
  246.   int len;
  247.   char *name;
  248.   char *origname;
  249.  
  250.   init_minimal_symbol_collection ();
  251.   make_cleanup (discard_minimal_symbols, 0);
  252.  
  253.   while (rtc_symp)
  254.     {
  255.       read_memory ((CORE_ADDR) rtc_symp,
  256.            (char *) &inferior_rtc_symb,
  257.            sizeof (inferior_rtc_symb));
  258.       read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
  259.            (char *) &inferior_rtc_nlist,
  260.            sizeof(inferior_rtc_nlist));
  261.       if (inferior_rtc_nlist.n_type == N_COMM)
  262.     {
  263.       /* FIXME: The length of the symbol name is not available, but in the
  264.          current implementation the common symbol is allocated immediately
  265.          behind the name of the symbol. */
  266.       len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
  267.  
  268.       origname = name = xmalloc (len);
  269.       read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
  270.  
  271.       /* Don't enter the symbol twice if the target is re-run. */
  272.  
  273. #ifdef NAMES_HAVE_UNDERSCORE
  274.       if (*name == '_')
  275.         {
  276.           name++;
  277.         }
  278. #endif
  279.       /* FIXME:  Do we really want to exclude symbols which happen
  280.          to match symbols for other locations in the inferior's
  281.          address space, even when they are in different linkage units? */
  282.       if (lookup_minimal_symbol (name, (struct objfile *) NULL) == NULL)
  283.         {
  284.           name = obsavestring (name, strlen (name),
  285.                    &objfile -> symbol_obstack);
  286.           prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
  287.                       mst_bss);
  288.         }
  289.       free (origname);
  290.     }
  291.       rtc_symp = inferior_rtc_symb.rtc_next;
  292.     }
  293.  
  294.   /* Install any minimal symbols that have been collected as the current
  295.      minimal symbols for this objfile. */
  296.  
  297.   install_minimal_symbols (objfile);
  298. }
  299.  
  300. #endif    /* SVR4_SHARED_LIBS */
  301.  
  302. #ifdef SVR4_SHARED_LIBS
  303.  
  304. /*
  305.  
  306. LOCAL FUNCTION
  307.  
  308.     bfd_lookup_symbol -- lookup the value for a specific symbol
  309.  
  310. SYNOPSIS
  311.  
  312.     CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
  313.  
  314. DESCRIPTION
  315.  
  316.     An expensive way to lookup the value of a single symbol for
  317.     bfd's that are only temporary anyway.  This is used by the
  318.     shared library support to find the address of the debugger
  319.     interface structures in the shared library.
  320.  
  321.     Note that 0 is specifically allowed as an error return (no
  322.     such symbol).
  323.  
  324.     FIXME:  See if there is a less "expensive" way of doing this.
  325.     Also see if there is already another bfd or gdb function
  326.     that specifically does this, and if so, use it.
  327. */
  328.  
  329. static CORE_ADDR
  330. bfd_lookup_symbol (abfd, symname)
  331.      bfd *abfd;
  332.      char *symname;
  333. {
  334.   unsigned int storage_needed;
  335.   asymbol *sym;
  336.   asymbol **symbol_table;
  337.   unsigned int number_of_symbols;
  338.   unsigned int i;
  339.   struct cleanup *back_to;
  340.   CORE_ADDR symaddr = 0;
  341.   
  342.   storage_needed = get_symtab_upper_bound (abfd);
  343.  
  344.   if (storage_needed > 0)
  345.     {
  346.       symbol_table = (asymbol **) xmalloc (storage_needed);
  347.       back_to = make_cleanup (free, (PTR)symbol_table);
  348.       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); 
  349.   
  350.       for (i = 0; i < number_of_symbols; i++)
  351.     {
  352.       sym = *symbol_table++;
  353.       if (strcmp (sym -> name, symname) == 0)
  354.         {
  355.           symaddr = sym -> value;
  356.           break;
  357.         }
  358.     }
  359.       do_cleanups (back_to);
  360.     }
  361.   return (symaddr);
  362. }
  363.  
  364. /*
  365.  
  366. LOCAL FUNCTION
  367.  
  368.     look_for_base -- examine file for each mapped address segment
  369.  
  370. SYNOPSYS
  371.  
  372.     static int look_for_base (int fd, CORE_ADDR baseaddr)
  373.  
  374. DESCRIPTION
  375.  
  376.     This function is passed to proc_iterate_over_mappings, which
  377.     causes it to get called once for each mapped address space, with
  378.     an open file descriptor for the file mapped to that space, and the
  379.     base address of that mapped space.
  380.  
  381.     Our job is to find the symbol DEBUG_BASE in the file that this
  382.     fd is open on, if it exists, and if so, initialize the dynamic
  383.     linker structure base address debug_base.
  384.  
  385.     Note that this is a computationally expensive proposition, since
  386.     we basically have to open a bfd on every call, so we specifically
  387.     avoid opening the exec file.
  388.  */
  389.  
  390. static int
  391. look_for_base (fd, baseaddr)
  392.      int fd;
  393.      CORE_ADDR baseaddr;
  394. {
  395.   bfd *interp_bfd;
  396.   CORE_ADDR address;
  397.  
  398.   /* If the fd is -1, then there is no file that corresponds to this
  399.      mapped memory segment, so skip it.  Also, if the fd corresponds
  400.      to the exec file, skip it as well. */
  401.  
  402.   if ((fd == -1) || fdmatch (fileno ((FILE *)(exec_bfd -> iostream)), fd))
  403.     {
  404.       return (0);
  405.     }
  406.  
  407.   /* Try to open whatever random file this fd corresponds to.  Note that
  408.      we have no way currently to find the filename.  Don't gripe about
  409.      any problems we might have, just fail. */
  410.  
  411.   if ((interp_bfd = bfd_fdopenr ("unnamed", NULL, fd)) == NULL)
  412.     {
  413.       return (0);
  414.     }
  415.   if (!bfd_check_format (interp_bfd, bfd_object))
  416.     {
  417.       bfd_close (interp_bfd);
  418.       return (0);
  419.     }
  420.  
  421.   /* Now try to find our DEBUG_BASE symbol in this file, which we at
  422.      least know to be a valid ELF executable or shared library. */
  423.  
  424.   if ((address = bfd_lookup_symbol (interp_bfd, DEBUG_BASE)) == 0)
  425.     {
  426.       bfd_close (interp_bfd);
  427.       return (0);
  428.     }
  429.  
  430.   /* Eureka!  We found the symbol.  But now we may need to relocate it
  431.      by the base address.  If the symbol's value is less than the base
  432.      address of the shared library, then it hasn't yet been relocated
  433.      by the dynamic linker, and we have to do it ourself.  FIXME: Note
  434.      that we make the assumption that the first segment that corresponds
  435.      to the shared library has the base address to which the library
  436.      was relocated. */
  437.  
  438.   if (address < baseaddr)
  439.     {
  440.       address += baseaddr;
  441.     }
  442.   debug_base = address;
  443.   bfd_close (interp_bfd);
  444.   return (1);
  445. }
  446.  
  447. #endif
  448.  
  449. /*
  450.  
  451. LOCAL FUNCTION
  452.  
  453.     locate_base -- locate the base address of dynamic linker structs
  454.  
  455. SYNOPSIS
  456.  
  457.     CORE_ADDR locate_base (void)
  458.  
  459. DESCRIPTION
  460.  
  461.     For both the SunOS and SVR4 shared library implementations, if the
  462.     inferior executable has been linked dynamically, there is a single
  463.     address somewhere in the inferior's data space which is the key to
  464.     locating all of the dynamic linker's runtime structures.  This
  465.     address is the value of the symbol defined by the macro DEBUG_BASE.
  466.     The job of this function is to find and return that address, or to
  467.     return 0 if there is no such address (the executable is statically
  468.     linked for example).
  469.  
  470.     For SunOS, the job is almost trivial, since the dynamic linker and
  471.     all of it's structures are statically linked to the executable at
  472.     link time.  Thus the symbol for the address we are looking for has
  473.     already been added to the minimal symbol table for the executable's
  474.     objfile at the time the symbol file's symbols were read, and all we
  475.     have to do is look it up there.  Note that we explicitly do NOT want
  476.     to find the copies in the shared library.
  477.  
  478.     The SVR4 version is much more complicated because the dynamic linker
  479.     and it's structures are located in the shared C library, which gets
  480.     run as the executable's "interpreter" by the kernel.  We have to go
  481.     to a lot more work to discover the address of DEBUG_BASE.  Because
  482.     of this complexity, we cache the value we find and return that value
  483.     on subsequent invocations.  Note there is no copy in the executable
  484.     symbol tables.
  485.  
  486.     Note that we can assume nothing about the process state at the time
  487.     we need to find this address.  We may be stopped on the first instruc-
  488.     tion of the interpreter (C shared library), the first instruction of
  489.     the executable itself, or somewhere else entirely (if we attached
  490.     to the process for example).
  491.  
  492.  */
  493.  
  494. static CORE_ADDR
  495. locate_base ()
  496. {
  497.  
  498. #ifndef SVR4_SHARED_LIBS
  499.  
  500.   struct minimal_symbol *msymbol;
  501.   CORE_ADDR address = 0;
  502.  
  503.   /* For SunOS, we want to limit the search for DEBUG_BASE to the executable
  504.      being debugged, since there is a duplicate named symbol in the shared
  505.      library.  We don't want the shared library versions. */
  506.  
  507.   msymbol = lookup_minimal_symbol (DEBUG_BASE, symfile_objfile);
  508.   if ((msymbol != NULL) && (msymbol -> address != 0))
  509.     {
  510.       address = msymbol -> address;
  511.     }
  512.   return (address);
  513.  
  514. #else    /* SVR4_SHARED_LIBS */
  515.  
  516.   /* Check to see if we have a currently valid address, and if so, avoid
  517.      doing all this work again and just return the cached address.  If
  518.      we have no cached address, ask the /proc support interface to iterate
  519.      over the list of mapped address segments, calling look_for_base() for
  520.      each segment.  When we are done, we will have either found the base
  521.      address or not. */
  522.  
  523.   if (debug_base == 0)
  524.     {
  525.       proc_iterate_over_mappings (look_for_base);
  526.     }
  527.   return (debug_base);
  528.  
  529. #endif    /* !SVR4_SHARED_LIBS */
  530.  
  531. }
  532.  
  533. static struct link_map *
  534. first_link_map_member ()
  535. {
  536.   struct link_map *lm = NULL;
  537.  
  538. #ifndef SVR4_SHARED_LIBS
  539.  
  540.   read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
  541.   if (dynamic_copy.ld_version >= 2)
  542.     {
  543.       /* It is a version that we can deal with, so read in the secondary
  544.      structure and find the address of the link map list from it. */
  545.       read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
  546.            sizeof (struct link_dynamic_2));
  547.       lm = ld_2_copy.ld_loaded;
  548.     }
  549.  
  550. #else    /* SVR4_SHARED_LIBS */
  551.  
  552.   read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
  553.   lm = debug_copy.r_map;
  554.  
  555. #endif    /* !SVR4_SHARED_LIBS */
  556.  
  557.   return (lm);
  558. }
  559.  
  560. /*
  561.  
  562. LOCAL FUNCTION
  563.  
  564.     find_solib -- step through list of shared objects
  565.  
  566. SYNOPSIS
  567.  
  568.     struct so_list *find_solib (struct so_list *so_list_ptr)
  569.  
  570. DESCRIPTION
  571.  
  572.     This module contains the routine which finds the names of any
  573.     loaded "images" in the current process. The argument in must be
  574.     NULL on the first call, and then the returned value must be passed
  575.     in on subsequent calls. This provides the capability to "step" down
  576.     the list of loaded objects. On the last object, a NULL value is
  577.     returned.
  578.  
  579.     The arg and return value are "struct link_map" pointers, as defined
  580.     in <link.h>.
  581.  */
  582.  
  583. static struct so_list *
  584. find_solib (so_list_ptr)
  585.      struct so_list *so_list_ptr;    /* Last lm or NULL for first one */
  586. {
  587.   struct so_list *so_list_next = NULL;
  588.   struct link_map *lm = NULL;
  589.   struct so_list *new;
  590.   
  591.   if (so_list_ptr == NULL)
  592.     {
  593.       /* We are setting up for a new scan through the loaded images. */
  594.       if ((so_list_next = so_list_head) == NULL)
  595.     {
  596.       /* We have not already read in the dynamic linking structures
  597.          from the inferior, lookup the address of the base structure. */
  598.       debug_base = locate_base ();
  599.       if (debug_base > 0)
  600.         {
  601.           /* Read the base structure in and find the address of the first
  602.          link map list member. */
  603.           lm = first_link_map_member ();
  604.         }
  605.     }
  606.     }
  607.   else
  608.     {
  609.       /* We have been called before, and are in the process of walking
  610.      the shared library list.  Advance to the next shared object. */
  611.       if ((lm = LM_NEXT (so_list_ptr)) == NULL)
  612.     {
  613.       /* We have hit the end of the list, so check to see if any were
  614.          added, but be quiet if we can't read from the target any more. */
  615.       int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
  616.                        (char *) &(so_list_ptr -> lm),
  617.                        sizeof (struct link_map));
  618.       if (status == 0)
  619.         {
  620.           lm = LM_NEXT (so_list_ptr);
  621.         }
  622.       else
  623.         {
  624.           lm = NULL;
  625.         }
  626.     }
  627.       so_list_next = so_list_ptr -> next;
  628.     }
  629.   if ((so_list_next == NULL) && (lm != NULL))
  630.     {
  631.       /* Get next link map structure from inferior image and build a local
  632.      abbreviated load_map structure */
  633.       new = (struct so_list *) xmalloc (sizeof (struct so_list));
  634.       (void) memset ((char *) new, 0, sizeof (struct so_list));
  635.       new -> lmaddr = lm;
  636.       /* Add the new node as the next node in the list, or as the root
  637.      node if this is the first one. */
  638.       if (so_list_ptr != NULL)
  639.     {
  640.       so_list_ptr -> next = new;
  641.     }
  642.       else
  643.     {
  644.       so_list_head = new;
  645.     }      
  646.       so_list_next = new;
  647.       read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
  648.            sizeof (struct link_map));
  649.       /* For the SVR4 version, there is one entry that has no name
  650.      (for the inferior executable) since it is not a shared object. */
  651.       if (LM_NAME (new) != 0)
  652.     {
  653.       if (!target_read_string((CORE_ADDR) LM_NAME (new), new -> so_name,
  654.               MAX_PATH_SIZE - 1))
  655.           error ("find_solib: Can't read pathname for load map\n");
  656.       new -> so_name[MAX_PATH_SIZE - 1] = 0;
  657.       solib_map_sections (new);
  658.     }      
  659.     }
  660.   return (so_list_next);
  661. }
  662.  
  663. /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
  664.  
  665. static int
  666. symbol_add_stub (arg)
  667.      char *arg;
  668. {
  669.   register struct so_list *so = (struct so_list *) arg;    /* catch_errs bogon */
  670.   
  671.   so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
  672.                    (unsigned int) so -> textsection -> addr,
  673.                    0, 0, 0);
  674.   return (1);
  675. }
  676.  
  677. /*
  678.  
  679. GLOBAL FUNCTION
  680.  
  681.     solib_add -- add a shared library file to the symtab and section list
  682.  
  683. SYNOPSIS
  684.  
  685.     void solib_add (char *arg_string, int from_tty,
  686.             struct target_ops *target)
  687.  
  688. DESCRIPTION
  689.  
  690. */
  691.  
  692. void
  693. solib_add (arg_string, from_tty, target)
  694.      char *arg_string;
  695.      int from_tty;
  696.      struct target_ops *target;
  697. {    
  698.   register struct so_list *so = NULL;       /* link map state variable */
  699.   char *re_err;
  700.   int count;
  701.   int old;
  702.   
  703.   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
  704.     {
  705.       error ("Invalid regexp: %s", re_err);
  706.     }
  707.   
  708.   /* Getting new symbols may change our opinion about what is
  709.      frameless.  */
  710.   reinit_frame_cache ();
  711.   
  712.   while ((so = find_solib (so)) != NULL)
  713.     {
  714.       if (so -> so_name[0] && re_exec (so -> so_name))
  715.     {
  716.       if (so -> symbols_loaded)
  717.         {
  718.           if (from_tty)
  719.         {
  720.           printf ("Symbols already loaded for %s\n", so -> so_name);
  721.         }
  722.         }
  723.       else
  724.         {
  725.           catch_errors (symbol_add_stub, (char *) so,
  726.                 "Error while reading shared library symbols:\n");
  727.           
  728.           special_symbol_handling (so);
  729.           so -> symbols_loaded = 1;
  730.           so -> from_tty = from_tty;
  731.         }
  732.     }
  733.     }
  734.   
  735.   /* Now add the shared library sections to the section table of the
  736.      specified target, if any.  */
  737.   if (target)
  738.     {
  739.       /* Count how many new section_table entries there are.  */
  740.       so = NULL;
  741.       count = 0;
  742.       while ((so = find_solib (so)) != NULL)
  743.     {
  744.       if (so -> so_name[0])
  745.         {
  746.           count += so -> sections_end - so -> sections;
  747.         }
  748.     }
  749.       
  750.       if (count)
  751.     {
  752.       /* Reallocate the target's section table including the new size.  */
  753.       if (target -> to_sections)
  754.         {
  755.           old = target -> to_sections_end - target -> to_sections;
  756.           target -> to_sections = (struct section_table *)
  757.         realloc ((char *)target -> to_sections,
  758.              (sizeof (struct section_table)) * (count + old));
  759.         }
  760.       else
  761.         {
  762.           old = 0;
  763.           target -> to_sections = (struct section_table *)
  764.         malloc ((sizeof (struct section_table)) * count);
  765.         }
  766.       target -> to_sections_end = target -> to_sections + (count + old);
  767.       
  768.       /* Add these section table entries to the target's table.  */
  769.       while ((so = find_solib (so)) != NULL)
  770.         {
  771.           if (so -> so_name[0])
  772.         {
  773.           count = so -> sections_end - so -> sections;
  774.           bcopy (so -> sections, (char *)(target -> to_sections + old), 
  775.              (sizeof (struct section_table)) * count);
  776.           old += count;
  777.         }
  778.         }
  779.     }
  780.     }
  781. }
  782.  
  783. /*
  784.  
  785. LOCAL FUNCTION
  786.  
  787.     info_sharedlibrary_command -- code for "info sharedlibrary"
  788.  
  789. SYNOPSIS
  790.  
  791.     static void info_sharedlibrary_command ()
  792.  
  793. DESCRIPTION
  794.  
  795.     Walk through the shared library list and print information
  796.     about each attached library.
  797. */
  798.  
  799. static void
  800. info_sharedlibrary_command (ignore, from_tty)
  801.      char *ignore;
  802.      int from_tty;
  803. {
  804.   register struct so_list *so = NULL;      /* link map state variable */
  805.   int header_done = 0;
  806.   
  807.   if (exec_bfd == NULL)
  808.     {
  809.       printf ("No exec file.\n");
  810.       return;
  811.     }
  812.   while ((so = find_solib (so)) != NULL)
  813.     {
  814.       if (so -> so_name[0])
  815.     {
  816.       if (!header_done)
  817.         {
  818.           printf("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
  819.              "Shared Object Library");
  820.           header_done++;
  821.         }
  822.       printf ("%-12s", local_hex_string_custom ((int) LM_ADDR (so), "08"));
  823.       printf ("%-12s", local_hex_string_custom (so -> lmend, "08"));
  824.       printf ("%-12s", so -> symbols_loaded ? "Yes" : "No");
  825.       printf ("%s\n",  so -> so_name);
  826.     }
  827.     }
  828.   if (so_list_head == NULL)
  829.     {
  830.       printf ("No shared libraries loaded at this time.\n");    
  831.     }
  832. }
  833.  
  834. /*
  835.  
  836. GLOBAL FUNCTION
  837.  
  838.     solib_address -- check to see if an address is in a shared lib
  839.  
  840. SYNOPSIS
  841.  
  842.     int solib_address (CORE_ADDR address)
  843.  
  844. DESCRIPTION
  845.  
  846.     Provides a hook for other gdb routines to discover whether or
  847.     not a particular address is within the mapped address space of
  848.     a shared library.  Any address between the base mapping address
  849.     and the first address beyond the end of the last mapping, is
  850.     considered to be within the shared library address space, for
  851.     our purposes.
  852.  
  853.     For example, this routine is called at one point to disable
  854.     breakpoints which are in shared libraries that are not currently
  855.     mapped in.
  856.  */
  857.  
  858. int
  859. solib_address (address)
  860.      CORE_ADDR address;
  861. {
  862.   register struct so_list *so = 0;       /* link map state variable */
  863.   
  864.   while ((so = find_solib (so)) != NULL)
  865.     {
  866.       if (so -> so_name[0])
  867.     {
  868.       if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
  869.           (address < (CORE_ADDR) so -> lmend))
  870.         {
  871.           return (1);
  872.         }
  873.     }
  874.     }
  875.   return (0);
  876. }
  877.  
  878. /* Called by free_all_symtabs */
  879.  
  880. void 
  881. clear_solib()
  882. {
  883.   struct so_list *next;
  884.   
  885.   while (so_list_head)
  886.     {
  887.       if (so_list_head -> sections)
  888.     {
  889.       free ((PTR)so_list_head -> sections);
  890.     }
  891.       if (so_list_head -> so_bfd)
  892.     {
  893.       bfd_close (so_list_head -> so_bfd);
  894.     }
  895.       next = so_list_head -> next;
  896.       free((PTR)so_list_head);
  897.       so_list_head = next;
  898.     }
  899.   debug_base = 0;
  900. }
  901.  
  902. /*
  903.  
  904. LOCAL FUNCTION
  905.  
  906.     disable_break -- remove the "mapping changed" breakpoint
  907.  
  908. SYNOPSIS
  909.  
  910.     static int disable_break ()
  911.  
  912. DESCRIPTION
  913.  
  914.     Removes the breakpoint that gets hit when the dynamic linker
  915.     completes a mapping change.
  916.  
  917. */
  918.  
  919. static int
  920. disable_break ()
  921. {
  922.   int status = 1;
  923.  
  924. #ifndef SVR4_SHARED_LIBS
  925.  
  926.   int in_debugger = 0;
  927.   
  928.   /* Read the debugger structure from the inferior to retrieve the
  929.      address of the breakpoint and the original contents of the
  930.      breakpoint address.  Remove the breakpoint by writing the original
  931.      contents back. */
  932.  
  933.   read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
  934.  
  935.   /* Set `in_debugger' to zero now. */
  936.  
  937.   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
  938.  
  939.   breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
  940.   write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
  941.         sizeof (debug_copy.ldd_bp_inst));
  942.  
  943. #else    /* SVR4_SHARED_LIBS */
  944.  
  945.   /* Note that breakpoint address and original contents are in our address
  946.      space, so we just need to write the original contents back. */
  947.  
  948.   if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
  949.     {
  950.       status = 0;
  951.     }
  952.  
  953. #endif    /* !SVR4_SHARED_LIBS */
  954.  
  955.   /* For the SVR4 version, we always know the breakpoint address.  For the
  956.      SunOS version we don't know it until the above code is executed.
  957.      Grumble if we are stopped anywhere besides the breakpoint address. */
  958.  
  959.   if (stop_pc != breakpoint_addr)
  960.     {
  961.       warning ("stopped at unknown breakpoint while handling shared libraries");
  962.     }
  963.  
  964.   return (status);
  965. }
  966.  
  967. /*
  968.  
  969. LOCAL FUNCTION
  970.  
  971.     enable_break -- arrange for dynamic linker to hit breakpoint
  972.  
  973. SYNOPSIS
  974.  
  975.     int enable_break (void)
  976.  
  977. DESCRIPTION
  978.  
  979.     Both the SunOS and the SVR4 dynamic linkers have, as part of their
  980.     debugger interface, support for arranging for the inferior to hit
  981.     a breakpoint after mapping in the shared libraries.  This function
  982.     enables that breakpoint.
  983.  
  984.     For SunOS, there is a special flag location (in_debugger) which we
  985.     set to 1.  When the dynamic linker sees this flag set, it will set
  986.     a breakpoint at a location known only to itself, after saving the
  987.     original contents of that place and the breakpoint address itself,
  988.     in it's own internal structures.  When we resume the inferior, it
  989.     will eventually take a SIGTRAP when it runs into the breakpoint.
  990.     We handle this (in a different place) by restoring the contents of
  991.     the breakpointed location (which is only known after it stops),
  992.     chasing around to locate the shared libraries that have been
  993.     loaded, then resuming.
  994.  
  995.     For SVR4, the debugger interface structure contains a member (r_brk)
  996.     which is statically initialized at the time the shared library is
  997.     built, to the offset of a function (_r_debug_state) which is guaran-
  998.     teed to be called once before mapping in a library, and again when
  999.     the mapping is complete.  At the time we are examining this member,
  1000.     it contains only the unrelocated offset of the function, so we have
  1001.     to do our own relocation.  Later, when the dynamic linker actually
  1002.     runs, it relocates r_brk to be the actual address of _r_debug_state().
  1003.  
  1004.     The debugger interface structure also contains an enumeration which
  1005.     is set to either RT_ADD or RT_DELETE prior to changing the mapping,
  1006.     depending upon whether or not the library is being mapped or unmapped,
  1007.     and then set to RT_CONSISTENT after the library is mapped/unmapped.
  1008. */
  1009.  
  1010. static int
  1011. enable_break ()
  1012. {
  1013.  
  1014.   int j;
  1015.  
  1016. #ifndef SVR4_SHARED_LIBS
  1017.  
  1018.   int in_debugger;
  1019.   
  1020.   /* Get link_dynamic structure */
  1021.  
  1022.   j = target_read_memory (debug_base, (char *) &dynamic_copy,
  1023.               sizeof (dynamic_copy));
  1024.   if (j)
  1025.     {
  1026.       /* unreadable */
  1027.       return (0);
  1028.     }
  1029.  
  1030.   /* Calc address of debugger interface structure */
  1031.  
  1032.   debug_addr = (CORE_ADDR) dynamic_copy.ldd;
  1033.  
  1034.   /* Calc address of `in_debugger' member of debugger interface structure */
  1035.  
  1036.   flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
  1037.                     (char *) &debug_copy);
  1038.  
  1039.   /* Write a value of 1 to this member.  */
  1040.  
  1041.   in_debugger = 1;
  1042.  
  1043.   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
  1044.  
  1045. #else    /* SVR4_SHARED_LIBS */
  1046.  
  1047. #ifdef BKPT_AT_MAIN
  1048.  
  1049.   struct minimal_symbol *msymbol;
  1050.  
  1051.   msymbol = lookup_minimal_symbol ("main", symfile_objfile);
  1052.   if ((msymbol != NULL) && (msymbol -> address != 0))
  1053.     {
  1054.       breakpoint_addr = msymbol -> address;
  1055.     }
  1056.   else
  1057.     {
  1058.       return (0);
  1059.     }
  1060.  
  1061.   if (target_insert_breakpoint (breakpoint_addr, shadow_contents) != 0)
  1062.     {
  1063.       return (0);
  1064.     }
  1065.  
  1066. #else    /* !BKPT_AT_MAIN */
  1067.  
  1068.   struct symtab_and_line sal;
  1069.  
  1070.   /* Read the debugger interface structure directly. */
  1071.  
  1072.   read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
  1073.  
  1074.   /* Set breakpoint at the debugger interface stub routine that will
  1075.      be called just prior to each mapping change and again after the
  1076.      mapping change is complete.  Set up the (nonexistent) handler to
  1077.      deal with hitting these breakpoints.  (FIXME). */
  1078.  
  1079.   warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
  1080.  
  1081. #endif    /* BKPT_AT_MAIN */
  1082.  
  1083. #endif    /* !SVR4_SHARED_LIBS */
  1084.  
  1085.   return (1);
  1086. }
  1087.   
  1088. /*
  1089.   
  1090. GLOBAL FUNCTION
  1091.   
  1092.     solib_create_inferior_hook -- shared library startup support
  1093.   
  1094. SYNOPSIS
  1095.   
  1096.     void solib_create_inferior_hook()
  1097.   
  1098. DESCRIPTION
  1099.   
  1100.     When gdb starts up the inferior, it nurses it along (through the
  1101.     shell) until it is ready to execute it's first instruction.  At this
  1102.     point, this function gets called via expansion of the macro
  1103.     SOLIB_CREATE_INFERIOR_HOOK.
  1104.  
  1105.     For both SunOS shared libraries, and SVR4 shared libraries, we
  1106.     can arrange to cooperate with the dynamic linker to discover the
  1107.     names of shared libraries that are dynamically linked, and the
  1108.     base addresses to which they are linked.
  1109.  
  1110.     This function is responsible for discovering those names and
  1111.     addresses, and saving sufficient information about them to allow
  1112.     their symbols to be read at a later time.
  1113.  
  1114. FIXME
  1115.  
  1116.     Between enable_break() and disable_break(), this code does not
  1117.     properly handle hitting breakpoints which the user might have
  1118.     set in the startup code or in the dynamic linker itself.  Proper
  1119.     handling will probably have to wait until the implementation is
  1120.     changed to use the "breakpoint handler function" method.
  1121.  
  1122.     Also, what if child has exit()ed?  Must exit loop somehow.
  1123.   */
  1124.  
  1125. void 
  1126. solib_create_inferior_hook()
  1127. {
  1128.   
  1129.   if ((debug_base = locate_base ()) == 0)
  1130.     {
  1131.       /* Can't find the symbol or the executable is statically linked. */
  1132.       return;
  1133.     }
  1134.  
  1135.   if (!enable_break ())
  1136.     {
  1137.       warning ("shared library handler failed to enable breakpoint");
  1138.       return;
  1139.     }
  1140.  
  1141.   /* Now run the target.  It will eventually hit the breakpoint, at
  1142.      which point all of the libraries will have been mapped in and we
  1143.      can go groveling around in the dynamic linker structures to find
  1144.      out what we need to know about them. */
  1145.  
  1146.   clear_proceed_status ();
  1147.   stop_soon_quietly = 1;
  1148.   stop_signal = 0;
  1149.   do
  1150.     {
  1151.       target_resume (0, stop_signal);
  1152.       wait_for_inferior ();
  1153.     }
  1154.   while (stop_signal != SIGTRAP);
  1155.   stop_soon_quietly = 0;
  1156.   
  1157.   /* We are now either at the "mapping complete" breakpoint (or somewhere
  1158.      else, a condition we aren't prepared to deal with anyway), so adjust
  1159.      the PC as necessary after a breakpoint, disable the breakpoint, and
  1160.      add any shared libraries that were mapped in. */
  1161.  
  1162.   if (DECR_PC_AFTER_BREAK)
  1163.     {
  1164.       stop_pc -= DECR_PC_AFTER_BREAK;
  1165.       write_register (PC_REGNUM, stop_pc);
  1166.     }
  1167.  
  1168.   if (!disable_break ())
  1169.     {
  1170.       warning ("shared library handler failed to disable breakpoint");
  1171.     }
  1172.  
  1173.   solib_add ((char *) 0, 0, (struct target_ops *) 0);
  1174. }
  1175.  
  1176. /*
  1177.  
  1178. LOCAL FUNCTION
  1179.  
  1180.     special_symbol_handling -- additional shared library symbol handling
  1181.  
  1182. SYNOPSIS
  1183.  
  1184.     void special_symbol_handling (struct so_list *so)
  1185.  
  1186. DESCRIPTION
  1187.  
  1188.     Once the symbols from a shared object have been loaded in the usual
  1189.     way, we are called to do any system specific symbol handling that 
  1190.     is needed.
  1191.  
  1192.     For Suns, this consists of grunging around in the dynamic linkers
  1193.     structures to find symbol definitions for "common" symbols and 
  1194.     adding them to the minimal symbol table for the corresponding
  1195.     objfile.
  1196.  
  1197. */
  1198.  
  1199. static void
  1200. special_symbol_handling (so)
  1201. struct so_list *so;
  1202. {
  1203. #ifndef SVR4_SHARED_LIBS
  1204.  
  1205.   /* Read the debugger structure from the inferior, just to make sure
  1206.      we have a current copy. */
  1207.  
  1208.   read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
  1209.  
  1210.   /* Get common symbol definitions for the loaded object. */
  1211.  
  1212.   if (debug_copy.ldd_cp)
  1213.     {
  1214.       solib_add_common_symbols (debug_copy.ldd_cp, so -> objfile);
  1215.     }
  1216.  
  1217. #endif    /* !SVR4_SHARED_LIBS */
  1218. }
  1219.  
  1220.  
  1221. /*
  1222.  
  1223. LOCAL FUNCTION
  1224.  
  1225.     sharedlibrary_command -- handle command to explicitly add library
  1226.  
  1227. SYNOPSIS
  1228.  
  1229.     static void sharedlibrary_command (char *args, int from_tty)
  1230.  
  1231. DESCRIPTION
  1232.  
  1233. */
  1234.  
  1235. static void
  1236. sharedlibrary_command (args, from_tty)
  1237. char *args;
  1238. int from_tty;
  1239. {
  1240.   dont_repeat ();
  1241.   solib_add (args, from_tty, (struct target_ops *) 0);
  1242. }
  1243.  
  1244. void
  1245. _initialize_solib()
  1246. {
  1247.   
  1248.   add_com ("sharedlibrary", class_files, sharedlibrary_command,
  1249.        "Load shared object library symbols for files matching REGEXP.");
  1250.   add_info ("sharedlibrary", info_sharedlibrary_command, 
  1251.         "Status of loaded shared object libraries.");
  1252. }
  1253.